//定义一个JS控制器 AMD require.js 模块化插件
//1、需要引入插件
//2、该控制器模块的业务逻辑
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form){
    // 定义一个控制器
    var Controller = {
        index:function(){
            //给窗口修改大小
            $(".btn-add").data('area', ['50%','90%'])
            // 初始化表格参数
            // 配置整个表格中增删改查请求控制器的地址，用的ajax情求
            Table.api.init({
                extend:{
                    index_url:'hotel/order/index',//列表查询的控制器地址
                    del_url:'hotel/order/del',//删除的控制器地址
                    info_url:'hotel/order/info',
                    checkin_url: 'hotel/order/checkin', // 办理入住
                    checkout_url: 'hotel/order/checkout', // 办理退房
                    defund_url : 'hotel/order/defund', // 审核是否通过退款
                    table:'hotel_order'//表
                }
            });

            // 获取view视图中的dom元素
            var table = $("#table")

            // 渲染表格
            // $.ajax
            table.bootstrapTable({
                url:$.fn.bootstrapTable.defaults.extend.index_url, //请求地址
                toolbar:".toolbar", //工具栏
                pk:'id',//默认主键字段名
                fixedColumns: true,
                fixedRightNumber: 1,
                // 渲染的数据部分
                columns:[
                    [// 渲染的字段部分
                        {checkbox:true},
                        // operate通用搜索的操作符，sortable切换排序
                        {field:'id',title:'ID',operate:false,sortable:true},
                        {field:'code',title:__('Code'),operate:'LIKE'},
                        {field:'business.nickname',title:__('Nickname'),operate:'LIKE'},
                        {field:'room.name',title:__('Room'),operate:'LIKE'},
                        {
                            field: 'coupon.title', 
                            title: __('Coupon'),
                            operate: false, 
                        },
                        {field:'guest_text',title:__('Guest'),operate:'LIKE'},
                        {field:'price',title:__('Price'),operate:false},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"1":__('已支付'),"2":__('已入住'),"3":__('已退房'),"4":__('已评价'),"-1":__('售后中'),"-2":__('退款成功'),"-3":__('退款失败')},
                            operate: 'LIKE',
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'createtime', 
                            title: __('CreateTime'), 
                            operate:'RANGE', addclass:'datetimerange', autocomplete:false, 
                            formatter: Table.api.formatter.datetime
                        },
                        // 最后一排操作按钮
                        {
                            field:'operate',
                            title:__('Operate'),
                            table:table,
                            events:Table.api.events.operate,
                            formatter:Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'info',
                                    title: '订单详情',
                                    extend: 'data-toggle="tooltip"',
                                    classname: "btn btn-xs btn-primary btn-dialog",
                                    icon: 'fa fa-eye',
                                    url: $.fn.bootstrapTable.defaults.extend.info_url,
                                },
                                {
                                    name:'checkin',
                                    title:'办理入住',
                                    classname:'btn btn-xs btn-success btn-magic btn-ajax',
                                    url:$.fn.bootstrapTable.defaults.extend.checkin_url,
                                    icon:'fa fa-leaf',
                                    extend:"data-toggle='tooltip'",
                                    confirm: "是否确认入住",
                                    success: function()
                                    {
                                        //刷新表格
                                        table.bootstrapTable('refresh')
                                    },
                                    visible:function(row){
                                        
                                        if(row.status == 1)
                                        {
                                            return true
                                        }
                                        return false
                                    }
                                },
                                {
                                    name:'checkout',
                                    title:'办理退房',
                                    classname:'btn btn-xs btn-success btn-magic btn-ajax',
                                    url:$.fn.bootstrapTable.defaults.extend.checkout_url,
                                    icon:'fa fa-user-times',
                                    extend:"data-toggle='tooltip'",
                                    confirm: "是否确认退房",
                                    success: function()
                                    {
                                        //刷新表格
                                        table.bootstrapTable('refresh')
                                    },
                                    visible:function(row){
                                        
                                        if(row.status == 2)
                                        {
                                            return true
                                        }
                                        return false
                                    }
                                },
                                {
                                    name:'defund',
                                    title: '退款审核',
                                    extend: 'data-toggle="tooltip"',
                                    classname: "btn btn-xs btn-primary btn-dialog",
                                    icon:'fa fa-check',
                                    url:$.fn.bootstrapTable.defaults.extend.defund_url,
                                    visible:function(row)
                                    {
                                        if(row.status == '-1')
                                        {
                                            return true;
                                        }

                                        return false;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function()
        {
            //给控制器绑定通用事件
            Controller.api.bindevent()
        },
        edit: function()
        {
            //给控制器绑定通用事件
            Controller.api.bindevent()
        },
        del: function()
        {
            //给控制器绑定通用事件
            Controller.api.bindevent()
        },
        // 办理入住
        checkin:function()
        {
            Controller.api.bindevent();
        },
        // 办理退房
        checkout:function()
        {
            Controller.api.bindevent();
        },
        // 退货审核
        defund:function(){

            $('#c-refund').change(function(){
                let val = $(this).val();

                if(val == 1)
                {
                    $('#examinereason').hide();
                    $('#c-examinereason').val('');
                }else{
                    $('#examinereason').show();
                }
            });

            Controller.api.bindevent();
        },
        api: {
            //JS模块化的全局方法
            bindevent: function () 
            {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    }
    //模块返回值
    return Controller
})